#include <graphics/PrimitiveDeclaration.h>

namespace ne
{
    PrimitiveDeclaration::PrimitiveDeclaration()
        : mTransformMatrix(Matrix4::IDENTITY),
          mPrimitiveType(PT_TRIANGLES)
    {
    }

    void PrimitiveDeclaration::setTransformMatrix(const Matrix4 &m)
    {
        mTransformMatrix = m;
    }

    const Matrix4& PrimitiveDeclaration::getTransformMatrix() const
    {
        return mTransformMatrix;
    }

    void PrimitiveDeclaration::setPrimitiveType(const PrimitiveType pt)
    {
        mPrimitiveType = pt;
    }

    PrimitiveType PrimitiveDeclaration::getPrimitiveType() const
    {
        return mPrimitiveType;
    }

    uint32_t PrimitiveDeclaration::getVertexCount() const
    {
        VertexAttributeMapConstIterator it = mVertexAttributeMap.find(VAS_POSITION);
        if (it != mVertexAttributeMap.end())
        {
            VertexAttributePtr pVertexArray = it->second;
            if (pVertexArray.notNull())
            {
                return pVertexArray->getCount();
            }
        }
        return 0;
    }

    bool PrimitiveDeclaration::hasVertexAttribute(const AttributeSemantic semantic) const
    {
        VertexAttributeMapConstIterator it = mVertexAttributeMap.find(semantic);
        if (it != mVertexAttributeMap.end())
        {
            return true;
        }
        return false;
    }

    VertexAttributePtr PrimitiveDeclaration::getVertexAttribute(const AttributeSemantic semantic) const
    {
        VertexAttributeMapConstIterator it = mVertexAttributeMap.find(semantic);
        if (it != mVertexAttributeMap.end())
        {
            return it->second;
        }
        return VertexAttributePtr();
    }

    bool PrimitiveDeclaration::addVertexAttribute(const VertexAttributePtr &pAttribute)
    {
        if (pAttribute.notNull())
        {
            mVertexAttributeMap[pAttribute->getSemantic()] = pAttribute;
            return true;
        }
        return false;
    }

    uint32_t PrimitiveDeclaration::getVertexAttributeCount() const
    {
        return uint32_t(mVertexAttributeMap.size());
    }

    const VertexAttributeMap& PrimitiveDeclaration::getVertexAttributeMap() const
    {
        return mVertexAttributeMap;
    }

    bool PrimitiveDeclaration::hasIndexAttribute() const
    {
        return !mIndexAttributeList.empty();
    }

    bool PrimitiveDeclaration::addIndexAttribute(const VertexAttributePtr &pAttribute)
    {
        if (pAttribute.notNull())
        {
            mIndexAttributeList.push_back(pAttribute);
            return true;
        }
        return false;
    }

    uint32_t PrimitiveDeclaration::getIndexAttributeCount() const
    {
        return uint32_t(mIndexAttributeList.size());
    }

    const VertexAttributeList& PrimitiveDeclaration::getIndexAttributeList() const
    {
        return mIndexAttributeList;
    }

    void PrimitiveDeclaration::removeAllVertexAttribute()
    {
        mVertexAttributeMap.clear();
    }

    void PrimitiveDeclaration::removeAllIndexAttribute()
    {
        mIndexAttributeList.clear();
    }

    void PrimitiveDeclaration::removeAll()
    {
        removeAllVertexAttribute();
        removeAllIndexAttribute();
    }

    PrimitiveDeclaration::~PrimitiveDeclaration()
    {
        removeAll();
    }
}
